package net.sf.midollo.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class UtilsImpl implements IUtilsImpl {

  public void log(String msg) {
    System.out.println(msg);
    try {
      MidpUtils.log(msg);
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }

  public List newList() {
    return newList(10);
  }

  public List newList(final int initialCapacity) {
    return new List() {
      private final java.util.Vector list = new java.util.Vector(initialCapacity);

      public boolean add(Object value) {
        list.addElement(value);
        return true;
      }

      public Object get(int index) {
        return list.elementAt(index);
      }

      public Object set(int index, Object value) {
        Object old = list.elementAt(index);
        list.setElementAt(value, index);
        return old;
      }

      public Object remove(int index) {
        Object obj = list.elementAt(index);
        list.removeElementAt(index);
        return obj;
      }

      public int size() {
        return list.size();
      }
    };
  }

  public Map newMap() {
    return new Map() {
      private final java.util.Hashtable map = new java.util.Hashtable();

      public Object get(Object key) {
        return map.get(key);
      }

      public Object put(Object key, Object value) {
        return map.put(key, value);
      }

      public Object remove(Object key) {
        return map.remove(key);
      }

      public int size() {
        return map.size();
      }
    };
  }

  public void writeTo(ByteArrayOutputStream baos, OutputStream os) throws IOException {
    os.write(baos.toByteArray());
  }
}
